home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3843 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  80 lines

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Want function of strmp with wild characters
  5. Date: Wed, 31 Jan 1996 16:04:03 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <310F76D3.2924@cmt.lpr.mail.carel.fi>
  8. References: <1996Jan30.213213.16764@schbbs.mot.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Ashok Patil wrote:
  16. > HI:
  17. > I need a function that compares two strings with
  18. > wild cards. like one string is "Hello" and another is
  19. > is "Helo*". The comparison of these two should return
  20. > 0. And if it works for ? that would be great.
  21. > If you have a function, please mail it to
  22. > Ashokman@aol.com
  23. > Thanks a lot
  24. > Ashok
  25.  
  26. You're welcome.
  27. int     WildCmp(const char *s1, const char *s2)
  28. {
  29.         while (*s1) {
  30.                 switch (*s1) {
  31.                         case '?':
  32.                                 break;
  33.                         case '*': {
  34.                                 char    tmp = *s2;
  35.  
  36.                                 s1++;
  37.                                 while (*s2 && *s2 != *s1)
  38.                                         s2++;
  39.                                 if (*s2 != *s1)
  40.                                         return (*s1 < tmp) ? -1 : 1;
  41.                                 }
  42.                                 if (!*s1)
  43.                                         return 0;
  44.                                 break;
  45.                         default:
  46.                                 if (*s1 < *s2)
  47.                                         return -1;
  48.                                 if (*s1 > *s2)
  49.                                         return 1;
  50.                 }
  51.                 if (!*s1 && *s2)
  52.                         return -1;
  53.                 if (*s1 && !*s2)
  54.                         return 1;
  55.                 s1++;
  56.                 s2++;
  57.         }
  58.         return (!*s1 && *s2) ? -1 : (*s1 && !*s2) ? 1 : 0;
  59. }
  60.  
  61. #ifdef  TEST
  62. #include        <stdio.h>
  63. #include        <assert.h>
  64. int     main(int argc, char **argv)
  65. {
  66.         assert(argc == 3);
  67.         printf("Str match: %d\n", WildCmp(argv[1], argv[2]));
  68.     return 0;
  69. }
  70. #endif
  71.  
  72. Later,
  73. AriL
  74. -- 
  75. All my opinions are mine and mine alone.
  76.